/**

 @author 石头哥哥
 </P>
 Date:   2014/12/24
 </P>
 Time:   19:34

 注解：

 */
package utils {

/**
 * 基于 array 构建
 */
public class ArrayList implements List {

    /**
     *   store object
     */
    private var _array:Array;

    /**
     *  size of list
     */
    private var _size:int = 0;

    /**
     * pointer
     */
    private var _index:int = 0;

    public function ArrayList() {
        _array = [];
    }

    /**
     * @param index
     * @return
     */
    public function getObject(index:int):* {
        if (index >= _index)throw Error("index out of bound, the index must be less:" + (_size - 1));
        return _array[index];
    }


    /**
     *
     * @param object
     */
    public function add(object:Object):void {
        _array[_index++] = object;
        ++_size;
    }

    /**
     *
     * @param objects
     */
    public function addAll(...objects):void {
        for each(var obj:Object in  objects) {
            add(obj);
        }
    }

    /**
     * delete index of object
     * @param index
     * @return
     */
    public function removeIndex(index:uint):* {
        if (index > _index)throw Error("index out bound of :" + _index);
        --_index;
        --_size;
        return _array.splice(index, 1);
    }

    /**
     * remove object
     * @param object
     * @return
     */
    public function removeObject(object:Object):* {
        return removeIndex( _array.indexOf(object));
    }


    public function size():uint {
        return _size;
    }


    /**
     * clear all
     */
    public function clear(dispose:Boolean = false):void {
        _array.splice(0, _size);
        _size = 0;
        _index = 0;
        if (dispose)_array = null;
    }


    /**
     * @return
     */
    public function toString():String {
        return _array.toString();
    }

    /**
     * add last
     * @param e
     */
    public function addLast(e:Object):void {
        add(e);
    }

    /**
     *
     * @param e
     */
    public function addFirst(e:Object):void {
        _array.unshift(e);
        ++_size;
        ++_index;
    }

    /**
     *
     * @return
     */
    public function pollLast():* {
        var o:Object = _array.pop();
        --_size;
        --_index;
        return o;
    }

    /**
     *
     * @return
     */
    public function pollFirst():* {
        var o:Object = _array.shift();
        --_size;
        --_index;
        return o;
    }
}
}
